<!--商品表单-->
<template>
  <div class="contain">
    <app-header></app-header>
    <!-- <router-link :to="{ path: '/desc', query: { url: 'imex' }}" class="tip-pc"><i class="iconfont icon-pc"></i>用PC端轻松完成商品批量导入</router-link> -->
    <div class="form-wrap">
      <!-- <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label"><span class="red">*</span>商品编号</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="goodNum"/></div>
        </div>
      </div> -->
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label"><span class="red">*</span>商品名称</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="goodName"/></div>
        </div>
      </div>
      <div class="form-choice line">
        <popup-picker class="vux-form-require" title="商品大类" :data="goodType.list" :columns="3" v-model="goodType.value" placeholder="点击选择" value-text-align="left" :show-name="goodType.show"></popup-picker>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label"><span class="red">*</span>单价</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="price"/></div>
          <em class="unit">元</em>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label"><span class="red">*</span>生产厂商</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="firm"/></div>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label"><span class="red">*</span>批准文号/注册证号</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="license"/></div>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label"><span class="red">*</span>商品规格</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="GoodsSpec"/></div>
        </div>
      </div>

      <uploader :title="'商品照片'" :img-list="imgList" :max="maxPic"></uploader>

    </div>
    <h2 class="form-tit-row">小包装</h2>
    <div class="form-wrap">
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label"><span class="red">*</span>包装含量</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="content"/></div>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label"><span class="red">*</span>包装单位</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="unit"/></div>
          <!-- <i class="iconfont icon-search"></i> -->
        </div>
      </div>
    </div>
    <h2 class="form-tit-row">大包装</h2>
    <div class="form-wrap">
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label">包装含量</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="maxContent"/></div>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label">包装单位</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="maxUnit"/></div>
          <!-- <i class="iconfont icon-search"></i> -->
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label">长</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="lengths"/></div>
          <em class="unit">cm</em>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label">宽</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="widths"/></div>
          <em class="unit">cm</em>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label">高</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="heights"/></div>
          <em class="unit">cm</em>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label">净重</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="weight"/></div>
          <em class="unit">kg</em>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label">毛重</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="rough"/></div>
          <em class="unit">kg</em>
        </div>
      </div>
    </div>
    <div class="form-btn" @click="submit">提交</div>
  </div>
</template>

<script>
import appHeader from '@/components/core/header/app_header.vue';
import uploader from '@/components/share/imageUploader/imageUploader.vue';
export default {
  components:{
    appHeader,
    uploader
  },
  data() {
    return {
      goodName: "",
      price: "",
      firm: "",
      license: "",
      content: "",
      unit: "",
      maxContent: "",
      maxUnit: "",
      lengths: "",
      widths: "",
      heights: "",
      weight: "",
      rough: "",
      goodType: {
        list: [
          {
            value: "04",
            name: "药品"
          },
          {
            value: "01",
            name: "普耗"
          },
          {
            value: "02",
            name: "跟台高值"
          },
          {
            value: "03",
            name: "备货高值"
          },
          {
            value: "05",
            name: "后勤物资"
          },
          {
            value: "06",
            name: "消毒用品"
          },
          {
            value: "07",
            name: "洗涤用品"
          },
          {
            value: "08",
            name: "中药材"
          },
          {
            value: "09",
            name: "设施设备"
          },
          {
            value: "10",
            name: "其他"
          }
        ],
        value: [],
        show: true
      },
      goodMsg: [],
      //图片
      imgList: [],
      maxPic:9,
      GoodsSpec: ""
    };
  },
  watch:{
  },
  created() {
    if (this.$route.query.guid) {
      this.getDate();
    }
  },
  mounted() {
    this.$nextTick(function() {});
  },
  methods: {
    getDate() {
      //渲染数据
      this.goodMsg = JSON.parse(sessionStorage.GoodItem);
      var goodMsg = this.goodMsg;
      // this.goodNum = goodMsg.GoodsCode;
      this.goodName = goodMsg.GoodsName;
      this.price = goodMsg.Price;
      this.firm = goodMsg.Producer;
      this.license = goodMsg.RegistKey;
      this.content = goodMsg.MinUnitQty;
      this.unit = goodMsg.MinUnitStyle;
      this.maxContent = goodMsg.BigUnitQty;
      this.maxUnit = goodMsg.BigUnitStyle;
      this.goodType.value.push(goodMsg.GoodsCate);
      this.lengths = goodMsg.Length;
      this.widths = goodMsg.Width;
      this.heights = goodMsg.Height;
      this.weight = goodMsg.NetWeight;
      this.rough = goodMsg.GrossWeight;
      this.GoodsSpec = goodMsg.GoodsSpec;

      var imgStr = goodMsg.PictContent;
      if (imgStr && imgStr !== "null" && imgStr !== '') {
        this.imgList = JSON.parse(imgStr)
      }
    },
    checkForm() {
      if (!this.goodName) {
        this.Toast({
          message: "请输入商品名称"
        });
        return false;
      }
      if (!this.goodType.value[0]) {
        this.Toast({
          message: "请选择商品大类"
        });
        return false;
      }
      if (!this.price) {
        this.Toast({
          message: "请输入单价"
        });
        return false;
      }
      if (!this.firm) {
        this.Toast({
          message: "请输入生产厂商"
        });
        return false;
      }
      if (!this.license) {
        this.Toast({
          message: "请输入批准文号/注册证号"
        });
        return false;
      }
      if (!this.GoodsSpec) {
        this.Toast({
          message: "请输入规格"
        });
        return false;
      }
      if (!this.content) {
        this.Toast({
          message: "请输入包装含量"
        });
        return false;
      }
      if (!this.unit) {
        this.Toast({
          message: "请输入包装单位"
        });
        return false;
      }
      return true;
    },
    submit() {
      var self = this;
      if (!this.checkForm()) {
        return false;
      }
      var userinfo = JSON.parse(localStorage.userinfo);
      var query = {
        TenantGuid: userinfo.TenantGuid,
        CorpGuid: userinfo.CorpGuid,
        GoodsName: this.goodName,
        GoodsCate: this.goodType.value[0],
        Price: this.price,
        Producer: this.firm,
        RegistKey: this.license,
        MinUnitQty: this.content,
        MinUnitStyle: this.unit,
        BigUnitQty: this.maxContent,
        BigUnitStyle: this.maxUnit,
        Length: this.lengths,
        Width: this.widths,
        Height: this.heights,
        NetWeight: this.weight,
        GrossWeight: this.rough,
        CreateUserID: userinfo.StaffID,
        CreateUserName: userinfo.Name,
        UpdateUserID: userinfo.StaffID,
        UpdateUserName: userinfo.Name,
        IsDeleted: "N",
        ImageArray: this.imgList,
        GoodsSpec: this.GoodsSpec,
        OrgRegistKey: ""
      };

      //query.ImageArray.push('')

      var method = "CreateMfgoods";
      if (this.$route.query.guid) {
        method = "UpdateMfgoods";
        //为了验证重复，如果REGISTKEY没改变，则不传
        if (query.RegistKey == this.goodMsg.RegistKey) {
          query.RegistKey = "";
          query.OrgRegistKey = this.goodMsg.RegistKey;
        }
        //end
        query.Guid = this.$route.query.guid;
      }
      this.Ajax.post(method, query).then((resp) => {
        if (resp.IsSuccess) {
          self.MessageBox.alert("保存成功").then(action => {
            self.$router.push({
              path: "/goods"
            });
          });
        } else {
          self.Toast({
            message: resp.ErrorMessage || "操作失败，请稍后重试"
          });
        }
      });
    },
  }
};
</script>